package org.scboy.dbms.valid.datatype;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.scboy.dbms.annotation.ValidType;
import org.scboy.dbms.entity.ParamConfig;
import org.scboy.dbms.valid.Valid;
import org.scboy.utils.entitys.coobi.Model;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;

@ValidType("整数")
public class IntValid implements Valid {

	@Override
	public List<ParamConfig> config() {
		return new ArrayList<ParamConfig>();
	}

	@Override
	public boolean valid(Object data,Model model, Model config) {
		String value=Convert.toStr(data);
		if(StrUtil.isNotEmpty(value)){
			Pattern p=Pattern.compile("^(0|[1-9][0-9]*|-[1-9][0-9]*)$");
			Matcher m=p.matcher(value);
			if(!m.matches()){
				return false;
			}
		}
		return true;
	}
	
	@Override
	public String toString(Model config) {
		return StrUtil.format("必须是整数");
	}
}
